package com.example.pingze.controller;

import com.example.pingze.entity.Sentence;
import com.example.pingze.entity.SentenceUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.SentenceRepository;
import com.example.pingze.service.SentenceUserCollectService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/sentenceUserCollect")
@CrossOrigin(origins = "*")
public class SentenceUserCollectController {
    @Autowired
    private SentenceUserCollectService service;
    @Autowired
    private SentenceRepository repository;
    //收藏
    @PostMapping("/collect")
    public Result<String> collect(@RequestParam String account,
                                  @RequestParam String sentenceId){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(sentenceId == null){
            throw new PingZeException("诗词账号为空");
        }
        SentenceUserCollect find = service.findSentenceUserCollectByAccountAndSentenceId(account, sentenceId);
        if(find == null){
            SentenceUserCollect collect = new SentenceUserCollect();
            collect.setAccount(account);
            collect.setSentenceId(sentenceId);
            collect.setId(UUID.randomUUID().toString());
            service.collect(collect);
        }else {
            throw new PingZeException("该诗句已被收藏");
        }

        return ResultVOUtils.success("收藏成功");
    }
    //取消收藏
    @PostMapping("/noCollect")
    public Result<String> noCollect(@RequestParam String account,
                                    @RequestParam String sentenceId){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(sentenceId == null){
            throw new PingZeException("诗词ID为空");
        }
        SentenceUserCollect collect = new SentenceUserCollect();
        collect.setAccount(account);
        collect.setSentenceId(sentenceId);
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        List<SentenceUserCollect> find = service.findSentenceUserCollect(collect);
        if(find != null){
            for (SentenceUserCollect list : find){
                service.delete(list);
            }
            return ResultVOUtils.success("取消收藏成功");
        }else{
            throw new PingZeException("用户的诗人信息未收藏");
        }
    }
    //返回收藏列表
    @PostMapping("/getSentenceList")
    public Result<List<Sentence>> getPoetList(@RequestParam String account){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        List<SentenceUserCollect> sentenceUserCollectByAccount = service.findSentenceUserCollectByAccount(account);
        if(sentenceUserCollectByAccount == null){
            throw new PingZeException("该用户该未收藏");
        }
        List<Sentence> sentenceList = new ArrayList<>();
        //遍历查询出来的收藏信息
        for(SentenceUserCollect list : sentenceUserCollectByAccount){
            String sentenceId = list.getSentenceId();
            if(sentenceId != null){
                Sentence sentence = repository.findSentenceById(sentenceId);
                if(sentence != null){
                    sentenceList.add(sentence);
                }
            }
        }
        return ResultVOUtils.result(sentenceList);
    }
    //返回状态
    @PostMapping("/getStatus")
    public Result<String> getStatus(@RequestParam String account,
                                    @RequestParam String sentenceId){
        String status = "";
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(sentenceId == null){
            throw new PingZeException("用户账号为空");
        }
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        SentenceUserCollect find = service.findSentenceUserCollectByAccountAndSentenceId(account, sentenceId);
        if(find == null){
            status = "no";
        }else{
            status = "ok";
        }
        return ResultVOUtils.success(status);
    }

    /**
     * 后台管理系统
     * @param sentenceUserCollect
     * @param pageCode
     * @param pageSize
     * @return
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<SentenceUserCollect>> getList(
            SentenceUserCollect sentenceUserCollect,
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order accountOrder = Sort.Order.asc("account");
        Sort sort = Sort.by(accountOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = service.getList(sentenceUserCollect, pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<SentenceUserCollect> add(String account, String sentenceId){
        SentenceUserCollect add = service.add(account, sentenceId);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<SentenceUserCollect> edit(String id, String account, String sentenceId){
        SentenceUserCollect edit = service.edit(id, account, sentenceId);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        SentenceUserCollect find = service.findSentenceUserCollectById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete successful");
    }
}
